//
//  CFSetDealPasswordVC.m
//  CFfuturestalent
//
//  Created by 子yu 刘 on 2018/4/27.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "CFSetDealPasswordVC.h"
#import "CFPasswordItme.h"
#import "CFUserinfoVC.h"

@interface CFSetDealPasswordVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UITextField *tfPassword;
@property (nonatomic ,assign) NSInteger strCount;
@property (nonatomic ,strong)UIButton * rightBtn;

@end

@implementation CFSetDealPasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addRightBtn];    
    [self setUpCollectionView];
    [self.tfPassword becomeFirstResponder];
    MJWeakSelf;
    [[self.tfPassword rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        if (x.length>=6) {
            weakSelf.tfPassword.text =  [weakSelf.tfPassword.text substringToIndex:6];
             [weakSelf.rightBtn setTitleColor:DEF_COLOR_GREEN forState:UIControlStateNormal];
        }else{
            [weakSelf.rightBtn setTitleColor:DEF_COLOR_DARKGREEN forState:UIControlStateNormal];
        }
        weakSelf.strCount = x.length;
        [weakSelf.collectionView reloadData];
    }];
}

- (void)addRightBtn{
    UIFont *titleFont = [UIFont systemFontOfSize:15];
    CGSize maxSize =  CGSizeMake(MAXFLOAT, MAXFLOAT);
    CGSize titleSize = [@"保存" boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:titleFont} context:nil].size;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn  setTitle:@"保存" forState:UIControlStateNormal];
    [btn setTitleColor:DEF_COLOR_GREEN forState:UIControlStateNormal];
    //设置字体大小
    btn.titleLabel.font = titleFont;
    btn.bounds = CGRectMake(0, 0, titleSize.width, titleSize.height);
    //事件
    [btn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    self.rightBtn = btn;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barBtnItemWithNmlImg:@"icon_back" hltImg:nil target:self action:@selector(back)];
}

-(void)back{
    [self popToUser];
}

- (void)save{
    if (self.tfPassword.text.length<6) {
        
    }else{
        [self popToUser];
    }
}

- (void)popToUser{
    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[CFUserinfoVC class]]) {
            [self.navigationController popToViewController:vc animated:YES];
        }
    }
}
- (void)setUpCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView.collectionViewLayout = layout;
    layout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
    layout.minimumLineSpacing = 1;
    layout.minimumInteritemSpacing = 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 6;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellID =@"CFPasswordItme";
    CFPasswordItme *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    if (indexPath.row>=self.strCount) {
        cell.pointView.hidden = YES;
    }else{
        cell.pointView.hidden = NO;
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(50,50);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self.tfPassword becomeFirstResponder];
}


@end
